home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_09_12
/
9n12105a
< prev
next >
Wrap
Text File
|
1991-04-13
|
2KB
|
63 lines
// Listing 4 - Functions to open and close files
// (See Listing 3 for the class definition)
// =============================================
// Function to open a file for class binaryfile
//
int binaryfile::fileopen (const char *n, enum FILEMODE m,
long h, enum SHAREMODE s) {
if (isopen() || !*n || record == NULL) return -2; // Error
// The O_type and S_type flags are defined in <sys\stat.h>
// The SH_type flags are Turbo C sharing flags from <share.h>
unsigned access = O_BINARY |
(m == Readonly ? O_RDONLY : O_RDWR) | // read/write access
(s == WriteShared ? SH_DENYNONE : // file sharing
(s == ReadShared ? SH_DENYWR : SH_DENYRW));
if (m == Append || m == Recreate) access |= O_APPEND;
handle = (m != Recreate ? open (n, access) :
open (n, access | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE));
if (handle < 0) return handle; // trap error
// set values into elements in the object
mode = m;
header = h;
recnbr = locked = 0L;
strncpy (name, n, 80); name[80] = '\0';
share = s;
countrecords();
return 0; // successful open
}
// =============================================
// Function to close a file for class binaryfile
//
int binaryfile::fileclose () {
int ret;
if (!isopen()) return 0; // already closed
if (locked > 0L) unlockrecord();
if ((ret = close (handle)) == 0) handle = -1;
return ret;
}
// =============================================
// Function to change the record length in
// class binaryfile
//
int binaryfile::changelength (unsigned len) {
void *ptr;
if (isopen() || !len || len > 4096) return -2; // Error
if ((ptr = realloc (record, len)) == NULL) return -1;
record = (char*) ptr; length = len;
return 0;
}
// =============================================
// Function to count the records in class binaryfile
//
long binaryfile::countrecords() {
return (count = (lseek (handle, 0, SEEK_END) - header) / length);
};